package com.fenghongzhang.kotlin1903.goods

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.fenghongzhang.kotlin1903.R
import com.fenghongzhang.kotlin1903.plus.toast
import kotlinx.android.synthetic.main.activity_goods.*
import org.w3c.dom.Text

class GoodsActivity : AppCompatActivity() {

    val viewModel:GoodsViewModel by lazy {
        ViewModelProvider(this).get(GoodsViewModel::class.java)

    }

    var index =1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_goods)

        val linearLayoutManager = LinearLayoutManager(this)
        goods_recycler.layoutManager = linearLayoutManager

        var list = mutableListOf<Goods>()

        val goodsAdapter = GoodsAdapter(list)

        goods_recycler.adapter = goodsAdapter

        viewModel.getGoods()

        viewModel.goods.observe(this){
            Log.i("haha","123")
            list.addAll(it)
            for(i in 0 until list.size){
                Log.i("list",list[i].toString())
            }
            goodsAdapter.notifyDataSetChanged()
        }


        goodsAdapter.onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener{ adapter,view,position ->
            index = position

            when(view.id){
                R.id.checkbox ->{
                    val checkBox = adapter.getViewByPosition(goods_recycler, position, R.id.checkbox) as CheckBox
                    if(checkBox.isChecked){
                        //总价格
//                        val count = if(countText.text.toString().toDouble() <= 0.0) 1.0 else countText.text.toString().toDouble()
                        val count =  countText.text.toString().toDouble()
                        //当前货物的价格
                        val goods = viewModel.goods.value?.get(position)!!
                        //当前加入购物车的数量
                        val goodsNum = adapter.getViewByPosition(goods_recycler,position, R.id.num) as TextView
                        var toInt = goodsNum.text.toString().toInt()
                        //一个产品的总价格
                        val times = goods?.let {
                            it.goods_price.toDouble().times(toInt)
                        }
                        //显示价格的控件
                        countText.text = (count + times).toString()
                    }else{
                        //总价格
                        val count = countText.text.toString().toDouble()
                        //当前货物的价格
                        val goods = viewModel.goods.value?.get(position)
                        //当前加入购物车的数量
                        val goodsNum = adapter.getViewByPosition(goods_recycler,position, R.id.num) as TextView
                        var toInt = goodsNum.text.toString().toInt()
                        //一个产品的总价格
                        val times = goods?.let {
                            it.goods_price.toDouble().times(toInt)
                        }
                        //显示价格的控件
                        countText.text = (count - times!!).toString()
                    }
                }
                R.id.add ->{
                    //加法
                    val goodsNum = adapter.getViewByPosition(goods_recycler,position, R.id.num) as TextView
                    var toInt = goodsNum.text.toString().toInt()
                    goodsNum.text = (toInt + 1).toString()

                    //更新viewModel中值
                    viewModel.update(position,goodsNum.text.toString())

                    //更新合计金额的显示
                    val checkBox = adapter.getViewByPosition(goods_recycler, position, R.id.checkbox) as CheckBox
                    //如果选中就更新金额,不选中不更新
                    if(checkBox.isChecked){
//                        "t".toast()
                        //总价格
                        val count = countText.text.toString().toDouble()
                        val goods = viewModel.goods.value?.get(position)!!
                        //显示价格的控件
                        countText.text = (count.toString().toDouble() + goods.goods_price.toDouble()).toString()
                    }



                }
                R.id.sub ->{

                    //加法
                    val goodsNum = adapter.getViewByPosition(goods_recycler,position, R.id.num) as TextView
                    var toInt = goodsNum.text.toString().toInt()
                    goodsNum.text = (toInt - 1).toString()
                    var num = toInt - 1
                    if(num <= 0){
                        return@OnItemChildClickListener
                    }
                    //更新viewModel中值
                    viewModel.update(position,goodsNum.text.toString())

                    //更新合计金额的显示
                    val checkBox = adapter.getViewByPosition(goods_recycler, position, R.id.checkbox) as CheckBox
                    //如果选中就更新金额,不选中不更新
                    if(checkBox.isChecked){
//                        "t".toast()
                        //总价格
                        val count = countText.text.toString().toDouble()
                        val goods = viewModel.goods.value?.get(position)
                        //显示价格的控件
                        countText.text = (count.toString().toDouble() - goods?.goods_price?.toDouble()!!).toString()
                    }


                }

            }





        }

        /**
         *  全选的逻辑
         *  1,把所有的多选框设置为选中
         *  2,累加所有价格,并改变合计的价格显示
         */
        selectAll.setOnCheckedChangeListener { _, boolean ->
            var count = 0.0

            if(boolean){
                for (i in 0 until (viewModel.goods.value?.size ?: 0)) {
                    if(viewModel.goods.value?.get(i)?.id == Const.Content){
                        //1 设置选中
                        var checkBox =  goodsAdapter.getViewByPosition(goods_recycler,i,R.id.checkbox) as CheckBox
                        checkBox.isChecked = true

                        //2 修改值
                        //数据的集合
                        val price = viewModel.goods.value?.get(i)?.goods_price
                        var textView =  goodsAdapter.getViewByPosition(goods_recycler,i,R.id.num) as TextView
                        count += (textView.text.toString().toDouble() * price?.toDouble()!!)
                    }
                }
            }else{
                for (i in 0 until (viewModel.goods.value?.size ?: 0)) {
                    if(viewModel.goods.value?.get(i)?.id == Const.Content){
                        //1 设置选中
                        var checkBox =  goodsAdapter.getViewByPosition(goods_recycler,i,R.id.checkbox) as CheckBox
                        checkBox.isChecked = false

                    }
                }

            }
            countText.text = count.toString()

        }




    }
}


